﻿// NaverSearchResult.cs
//

using System;
using System.ComponentModel.DataAnnotations;
using System.Xml.Linq;

namespace LinqProviders.LinqToNaver {

    public abstract class NaverSearchResult {

        // TODO: Need to get RIA Services to support Uri as a valid key type...
        //[Key]
        //[Display(AutoGenerateField = false)]
        public string ID {
            get;
            set;
        }

        //[Editable(false)]
        public Uri Uri {
            get;
            set;
        }

        //[Editable(false)]
        //[Display(AutoGenerateField = false)]
        public string Query {
            get;
            set;
        }

        public string Target { get; set; }
        /// <summary>
        /// Gets or sets the total.
        /// 검색 결과 문서의 총 개수를 의미합니다.
        /// </summary>
        /// <value>The total.</value>
        public int Total { get; set; }
        /// <summary>
        /// Gets or sets the start.
        /// 검색 결과 문서 중, 문서의 시작점을 의미합니다.
        /// </summary>
        /// <value>The start.</value>
        public int Start { get; set; }
        /// <summary>
        /// Gets or sets the display.
        /// 검색된 검색 결과의 개수입니다.
        /// </summary>
        /// <value>The display.</value>
        public int Display { get; set; }
        //public string Sort { get; set; }
        //public string Filter { get; set; }

        /// <summary>
        /// 웹 리퀘스트 코드는 OK 이지만 원격서버로 부터 Error 메세지 받았을때 담는 객체.
        /// </summary>
        /// <value>The error.</value>
        public ApiError Error { get; set; }


        protected abstract object Parse(string input, string requestedQuery, out int count, out int totalCount);
        /// <summary>
        /// Executs the specified URI.
        /// </summary>
        /// <param name="uri">The URI.</param>
        /// <param name="query">The query.</param>
        /// <returns></returns>
        public virtual object Execut(Uri uri, string query)
        {
            string responseData = LinqProviders.Utility.HttpReqeust.ExecuteWebRequest(uri);
            int count;
            int totalCount;
            return Parse(responseData, query, out count, out totalCount);
        }
             

    }

    public class Channel
    {
        public string Title { get; set; }
        public Uri Link { get; set; }
        public String Description { get; set; }
        public DateTime LastBuildDate { get; set; }

        public int Total { get; set; }
        public int Display { get; set; }
        public int Start { get; set; }
    }

    /// <summary>
    /// Api Error Class
    /// </summary>
    public class ApiError
    {
        public string Error_Code { get; set; }
        public string Message { get; set; }

        public ApiError(string errorCode, string message)
        {
            this.Error_Code = errorCode;
            this.Message = message;
        }

        public static ApiError Parse(System.Xml.Linq.XDocument doc)
        {
            XElement elem = doc.Element("error");
            if (elem == null)
                return null;

            return new ApiError(elem.Element("error_code").Value, elem.Element("message").Value); 

        }
    }

}
